<template>
	<view class="voucher-page">
		
		<!-- 代金券模块 -->
		<view class="voucher-section">
			<view class="section-header">
				<text class="section-title">我的代金券</text>
			</view>
			<button class="delete-btn" @click="deleteAllCoupons">删除所有代金券</button>

			<!-- 代金券列表 -->
			<view class="voucher-list">
				<view class="voucher-card" v-for="(voucher, index) in voucherList" :key="index"
					@click="useVoucher(voucher)">
					<!-- 代金券背景 -->
					<view class="voucher-bg" :style="{'background': voucher.bgColor}">
						<!-- 金额区域 -->
						<view class="voucher-amount">
							<text class="amount-sign">¥</text>
							<text class="amount-num">{{voucher.amount}}</text>
						</view>

						<!-- 信息区域 -->
						<view class="voucher-info">
							<text class="info-title">{{voucher.title}}</text>
							<text class="info-desc">{{voucher.desc}}</text>
							<text class="info-date">有效期至 {{voucher.endDate}}</text>
						</view>

						<!-- 标签 -->
						<view class="voucher-tag" :style="{'background': voucher.tagColor}">
							<text class="tag-text">{{voucher.tag}}</text>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 空状态提示 -->
			<view class="empty-state" v-if="voucherList.length === 0">
				<text>暂无可用代金券</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				voucherList: []
			};
		},
		onLoad() {
			this.loadCoupons();
		},
		onShow() {
			// 页面显示时重新加载优惠券，确保数据同步
			this.loadCoupons();
		},
		
		methods: {
			// 加载优惠券数据
			loadCoupons() {
				this.voucherList = uni.getStorageSync('myCoupons') || [];
				console.log('加载优惠券数据:', this.voucherList);
			},
			
			// 删除所有代金券
			deleteAllCoupons() {
				uni.showModal({
					title: '确认删除',
					content: '确定要删除所有代金券吗？',
					success: (res) => {
						if (res.confirm) {
							// 清空本地存储中的优惠券数据
							uni.setStorageSync('myCoupons', []);
							// 同时清空选中的优惠券
							uni.removeStorageSync('selectedVoucher');
							// 清空页面数据
							this.voucherList = [];
							uni.showToast({
								title: '所有代金券已删除',
								icon: 'none'
							});
						}
					}
				});
			},
			
			// 使用代金券
			// 在代金券页面的 useVoucher 方法中
			useVoucher(voucher) {
				console.log('选择优惠券:', voucher);
				
				// 1. 将选中的优惠券存入本地存储
				uni.setStorageSync("selectedVoucher", voucher);
				
				// 2. 获取商品总价（这里需要根据您的实际业务逻辑获取）
				const productTotal = 100; // 示例金额，实际应该从商品页面获取
				
				// 3. 显示选择成功提示
				uni.showToast({
					title: `已选择${voucher.amount}元代金券`,
					icon: 'success',
					duration: 1500
				});
				
				// 4. 延迟跳转，让用户看到提示
				setTimeout(() => {
					// 5. 跳转到结算页面，并传递商品总价
					uni.navigateBack({
						delta: 1
					});
				}, 1000);
			}
		}
	};
</script>

<style scoped>
	.voucher-section {
		padding: 20rpx;
		background-color: #fff;
		margin: 15rpx;
		border-radius: 24rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.06);
	}

	.section-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.section-title {
		font-size: 40rpx;
		font-weight: 600;
		color: #222;
	}
	
	.delete-btn {
		background-color: #ff4d4f;
		color: white;
		border: none;
		padding: 10rpx 20rpx;  
		border-radius: 8rpx;  
		margin: 20rpx;
		font-size: 24rpx;    
	}

	.voucher-list {
		display: flex;
		flex-direction: column;
		gap: 16rpx;
	}

	.voucher-card {
		width: 95%;
	}

	.voucher-bg {
		width: 100%;
		height: 220rpx;
		border-radius: 18rpx;
		padding: 24rpx;
		display: flex;
		align-items: center;
		position: relative;
		overflow: hidden;
	}

	/* 金额区域 */
	.voucher-amount {
		display: flex;
		align-items: flex-start;
		color: #fff;
		margin-right: 24rpx;
	}

	.amount-sign {
		font-size: 36rpx;
		margin-top: 12rpx;
	}

	.amount-num {
		font-size: 72rpx;
		font-weight: 700;
		line-height: 1;
	}

	/* 信息区域 */
	.voucher-info {
		flex: 1;
		color: #fff;
	}

	.info-title {
		font-size: 30rpx;
		font-weight: 600;
		margin-bottom: 10rpx;
		display: block;
	}

	.info-desc {
		font-size: 24rpx;
		opacity: 0.9;
		margin-bottom: 14rpx;
		display: block;
	}

	.info-date {
		font-size: 22rpx;
		opacity: 0.8;
		display: block;
	}

	/* 标签样式 */
	.voucher-tag {
		position: absolute;
		top: 24rpx;
		right: 24rpx;
		padding: 8rpx 16rpx;
		border-radius: 24rpx;
	}

	.tag-text {
		font-size: 22rpx;
		color: #fff;
	}
	
	/* 空状态样式 */
	.empty-state {
		text-align: center;
		padding: 60rpx 0;
		color: #999;
		font-size: 28rpx;
	}
</style>